﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechCraftEngine.WorldEngine.Generators;
using TechCraftEngine.WorldEngine;
using System.IO;

namespace Blox.Generators
{
    class FileLevelBuilder : IRegionBuilder
    {
        string[] Blocks;
        public World wrd { get; set; }

        public FileLevelBuilder(string levelpath, World wrld)
        {
            Blocks = File.ReadAllLines(levelpath);
            this.wrd = wrld;
        }

        public void build(Region chunk)
        {
            /*for (int x = 0; x < WorldSettings.REGIONWIDTH; x++)
            {
                int worldX = chunk.Position.x + x + WorldSettings.SEED;

                for (int z = 0; z < WorldSettings.REGIONLENGTH; z++)
                {
                    int worldZ = chunk.Position.z + z;
                    //generateTerrain(chunk, x, z, worldX, worldZ, WorldSettings.MAPHEIGHT);
                }
            }*/



            foreach (string x in Blocks)
            {
                string[] bx = x.Split(':');
                if (x.StartsWith("p"))
                {
                    float pX = float.Parse(bx[1]);
                    float pY = float.Parse(bx[2]);
                    float pZ = float.Parse(bx[3]);

                    Global.PlayerPos.X = pX;
                    Global.PlayerPos.Y = pY;
                    Global.PlayerPos.Z = pZ;
                }
                else
                {
                    int reX = int.Parse(bx[0]);
                    int reY = int.Parse(bx[1]);
                    int reZ = int.Parse(bx[2]);

                    int bX = int.Parse(bx[3]);
                    int bY = int.Parse(bx[4]);
                    int bZ = int.Parse(bx[5]);

                    int bT = int.Parse(bx[6]);

                    if (reX != chunk.Position.x)
                    {
                        continue;
                    }
                    else if (reY != chunk.Position.y)
                    {
                        continue;
                    }
                    else if (reZ != chunk.Position.z)
                    {
                        continue;
                    }
                    else
                    {
                        chunk.AddBlock(bX, bY, bZ, (BlockType)bT);
                    }
                }
            }




            chunk.BuildVertexBuffers();
            chunk.Dirty = false;

        }
    }
}
